<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/28
 * Time: 15:40
 */

namespace app\admin\controller;


use app\admin\model\MemberUser;
use app\admin\model\ShopList;
use app\admin\model\ShopTable;
use think\facade\Session;

class StaffController extends BaseController
{

    public function staffList()
    {
        $shop_id = Session::get('shop_id') ? ['id' => Session::get('shop_id')] : '';
        $data = ShopList::where($shop_id)->where('status', 1)->field('id,shop_name')->select()->toArray();
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function staffData($page, $limit)
    {
        $shop_id = input('get.shop_id');
        $data = MemberUser::where('shop_id', $shop_id)->where('status', 2)
            ->field('id,true_name,phone,job_number,shop_id,staff_status')->page($page, $limit)->select()->each(function ($item) {
                $table = ShopTable::where('shop_id', $item->shop_id)
                    ->where('waiter_id', $item->id)->where('status', 1)->select();
                if ($table) {
                    $arr = [];
                    foreach ($table as $k => $v) {
                        $arr[] = $v['table_no'];
                    }
                    $item->table_id = implode(',', $arr);
                } else {
                    $item->table_id = '';
                }
                $item->shop_id = ShopList::where('id', $item->shop_id)->value('shop_name');
            })->toArray();
        $count = MemberUser::where('shop_id', $shop_id)->where('status', 2)->count();
        $res = [
            'code' => 0,
            'msg' => '返回成功',
            'count' => $count,
            'data' => $data
        ];
        return json($res);
    }

    public function staffAdd()
    {
        $shop_id = Session::get('shop_id') ? ['id' => Session::get('shop_id')] : '';
        $data['list'] = ShopList::where($shop_id)->where('status', 1)->field('id,shop_name')
            ->select()->toArray();
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function staffTable()
    {
        $shop_id = (int)input('post.shop_id');
        $data = ShopTable::where('shop_id', $shop_id)->where('waiter_id', 0)->where('status', 1)->field('id,table_no')
            ->select()->toArray();
        if (!$data) {
            return json(['code' => 0, 'msg' => '店铺没有空余桌号']);
        }
        return json(['code' => 1, 'msg' => '返回成功', 'data' => $data]);
    }

    public function staffSave()
    {
        $data = input('post.');
        $user = MemberUser::get((int)$data['user_id']);
        if (!$user) {
            return json(['code' => 0, 'msg' => '员工不存在']);
        }
        $upData = [
            'phone' => $data['phone'],
            'job_number' => $data['job_number'],
            'shop_id' => $data['shop_id'],
            'true_name' => $data['true_name'],
            'status' => 2,
            'staff_status' => $data['staff_status']
        ];
        $waiter = MemberUser::update($upData, ['id' => $data['user_id']]);
        $table = ShopTable::update(['waiter_id' => $data['user_id']], ['id' => $data['table']]);
        if (!$waiter && !$table) {
            return json(['code' => 0, 'msg' => '保存失败']);
        }
        return json(['code' => 1, 'msg' => '保存成功']);
    }

    public function staffEdit(){
        $user_id = input('get.user_id');
        $data['info'] = MemberUser::where('id',$user_id)
            ->field('id,shop_id,true_name,job_number,phone,staff_status')->find()->toArray();
        $data['table'] = ShopTable::where('shop_id',$data['info']['shop_id'])
            ->where('status',1)
            ->where('waiter_id',0)
            ->whereOr('waiter_id',$user_id)
            ->field('id,table_no,waiter_id')->select()->toArray();
        $data['info']['shop_id'] = ShopList::where('id',$data['info']['shop_id'])
            ->field('id,shop_name')->find()->toArray();
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function staffUp(){
        $data = input('post.');
        ShopTable::update(['waiter_id'=>0],['shop_id'=>$data['shop_id'],'waiter_id' => $data['user_id']]);
        $upData = [
            'phone' => $data['phone'],
            'staff_status' => $data['staff_status']
        ];
        $waiter = MemberUser::update($upData, ['id' => $data['user_id']]);
        $table = ShopTable::update(['waiter_id' => $data['user_id']], ['id' => $data['table']]);
        if (!$waiter && !$table) {
            return json(['code' => 0, 'msg' => '保存失败']);
        }
        return json(['code' => 1, 'msg' => '保存成功']);
    }

    public function staffDel(){
        $id = input('post.id');
        ShopTable::update(['waiter_id'=>0],['waiter_id' => $id]);
        $ret = MemberUser::update(['status'=>1],['id'=>$id]);
        if (!$ret){
            return json(['code'=>0,'msg'=>'删除失败']);
        }
        return json(['code'=>1,'msg'=>'删除成功']);
    }

    public function cookList(){
        $shop_id = Session::get('shop_id') ? ['id' => Session::get('shop_id')] : '';
        $data = ShopList::where($shop_id)->where('status', 1)->field('id,shop_name')->select()->toArray();
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function cookData($page,$limit){
        $shop_id = input('get.shop_id');
        $data = MemberUser::where('shop_id', $shop_id)->where('status', 3)
            ->field('id,true_name,phone,job_number,shop_id,staff_status')
            ->page($page, $limit)->select()->each(function ($item) {
                $item->shop_id = ShopList::where('id', $item->shop_id)->value('shop_name');
            })->toArray();
        $count = MemberUser::where('shop_id', $shop_id)->where('status', 2)->count();
        $res = [
            'code' => 0,
            'msg' => '返回成功',
            'count' => $count,
            'data' => $data
        ];
        return json($res);
    }

    public function cookAdd(){
        $shop_id = Session::get('shop_id') ? ['id' => Session::get('shop_id')] : '';
        $data['list'] = ShopList::where($shop_id)->where('status', 1)
            ->field('id,shop_name')
            ->select()->toArray();
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function cookSave(){
        $data = input('post.');
        $user = MemberUser::get((int)$data['user_id']);
        if (!$user) {
            return json(['code' => 0, 'msg' => '员工不存在']);
        }
        $upData = [
            'phone' => $data['phone'],
            'job_number' => $data['job_number'],
            'shop_id' => $data['shop_id'],
            'true_name' => $data['true_name'],
            'status' => 3,
            'staff_status' => $data['staff_status']
        ];
        $waiter = MemberUser::update($upData, ['id' => $data['user_id']]);
        if (!$waiter) {
            return json(['code' => 0, 'msg' => '保存失败']);
        }
        return json(['code' => 1, 'msg' => '保存成功']);
    }

    public function cookEdit(){
        $user_id = input('get.user_id');
        $data['info'] = MemberUser::where('id',$user_id)
            ->field('id,shop_id,true_name,job_number,phone,staff_status')->find()->toArray();
        $data['info']['shop_id'] = ShopList::where('id',$data['info']['shop_id'])
            ->field('id,shop_name')->find()->toArray();
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function cookUp(){
        $data = input('post.');
        $upData = [
            'phone' => $data['phone'],
            'staff_status' => $data['staff_status']
        ];
        $waiter = MemberUser::update($upData, ['id' => $data['user_id']]);
        if (!$waiter) {
            return json(['code' => 0, 'msg' => '保存失败']);
        }
        return json(['code' => 1, 'msg' => '保存成功']);
    }


    public function cookDel(){
        $id = input('post.id');
        $ret = MemberUser::update(['status'=>1],['id'=>$id]);
        if (!$ret){
            return json(['code'=>0,'msg'=>'删除失败']);
        }
        return json(['code'=>1,'msg'=>'删除成功']);
    }

}